/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TroopsSaveDialog.java
 *
 * Created on 01.08.2009, 18:58:20
 */
package de.tor.tribes.dssim.ui;

import de.tor.tribes.dssim.io.SimIOHelper;
import de.tor.tribes.dssim.model.SimulatorTableModel;
import de.tor.tribes.dssim.types.AbstractUnitElement;
import de.tor.tribes.dssim.types.UnitHolder;
import java.awt.event.ItemEvent;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Charon
 */
public class TroopsSaveDialog extends javax.swing.JDialog {

    private final int SAVE_OFF_TYPE = 0;
    private final int SAVE_DEF_TYPE = 1;
    private static TroopsSaveDialog SINGLETON = null;
    private int type = SAVE_OFF_TYPE;

    public static synchronized TroopsSaveDialog getSingleton() {
        if (SINGLETON == null) {
            SINGLETON = new TroopsSaveDialog(DSWorkbenchSimulatorFrame.getSingleton(), true);
        }
        return SINGLETON;
    }

    public void showSaveDefDialog() {
        setTitle("Deff speichern");
        type = SAVE_DEF_TYPE;
        jNameField.setText("MeineDeff");
        jExistingSetups.setModel(new DefaultComboBoxModel(SimIOHelper.getDefSetups().toArray(new String[]{})));
        //jExistingSetups.setSelectedIndex(0);
        setLocationRelativeTo(DSWorkbenchSimulatorFrame.getSingleton());
        setVisible(true);
    }

    public void showSaveOffDialog() {
        setTitle("Off speichern");
        type = SAVE_OFF_TYPE;
        jNameField.setText("MeineOff");
        jExistingSetups.setModel(new DefaultComboBoxModel(SimIOHelper.getOffSetups().toArray(new String[]{})));
        //jExistingSetups.setSelectedIndex(0);
        setLocationRelativeTo(DSWorkbenchSimulatorFrame.getSingleton());
        setVisible(true);
    }

    /** Creates new form TroopsSaveDialog */
    TroopsSaveDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jNameField = new javax.swing.JTextField();
        jSaveButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jExistingSetups = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();

        setAlwaysOnTop(true);
        setModal(true);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Bezeichnung");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jLabel1, gridBagConstraints);

        jNameField.setMinimumSize(new java.awt.Dimension(180, 25));
        jNameField.setPreferredSize(new java.awt.Dimension(180, 25));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jNameField, gridBagConstraints);

        jSaveButton.setText("Speichern");
        jSaveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fireCloseEvent(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jSaveButton, gridBagConstraints);

        jButton2.setText("Abbrechen");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fireCloseEvent(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jButton2, gridBagConstraints);

        jExistingSetups.setMinimumSize(new java.awt.Dimension(180, 25));
        jExistingSetups.setPreferredSize(new java.awt.Dimension(180, 25));
        jExistingSetups.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                fireSetupChangedEvent(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jExistingSetups, gridBagConstraints);

        jLabel2.setText("Überschreiben");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jLabel2, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void fireCloseEvent(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fireCloseEvent
        String name = jNameField.getText();
        if (evt.getSource() == jSaveButton) {
            if (name.length() <= 0) {
                JOptionPane.showMessageDialog(this, "Ungültige Bezeichnung.", "Fehler", JOptionPane.ERROR_MESSAGE);
                return;
            }
            Hashtable<UnitHolder, AbstractUnitElement> toSave = null;
            if (type == SAVE_OFF_TYPE) {
                //save off
                name = name + "_off.xml";
                toSave = SimulatorTableModel.getSingleton().getOff();
            } else {
                //save def
                name = name + "_def.xml";
                toSave = SimulatorTableModel.getSingleton().getDef();
            }
            Enumeration<UnitHolder> keys = toSave.keys();
            List<AbstractUnitElement> elements = new LinkedList<AbstractUnitElement>();
            while (keys.hasMoreElements()) {
                elements.add(toSave.get(keys.nextElement()));
            }
            try {
                SimIOHelper.writeTroopSetup(elements, SimIOHelper.getDataDir() + "/" + name);
                JOptionPane.showMessageDialog(this, "Truppen erfolgreich gespeichert.", "Information", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Fehler beim Speichern der Truppen. (Grund: " + e.getMessage() + ")", "Fehler", JOptionPane.ERROR_MESSAGE);
            }
        }
        setVisible(false);
    }//GEN-LAST:event_fireCloseEvent

    private void fireSetupChangedEvent(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_fireSetupChangedEvent
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            jNameField.setText((String) jExistingSetups.getSelectedItem());
        }
    }//GEN-LAST:event_fireSetupChangedEvent

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TroopsSaveDialog dialog = new TroopsSaveDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jExistingSetups;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jNameField;
    private javax.swing.JButton jSaveButton;
    // End of variables declaration//GEN-END:variables
}
